package com.mystyle.le.weekproject.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.activity.DetialActivity;
import com.mystyle.le.weekproject.adapter.SeniorityFragmentAdapter;
import com.mystyle.le.weekproject.asynctaks.SeniorityFragmentAsynctaks;
import com.mystyle.le.weekproject.beans.Seniority;
import com.mystyle.le.weekproject.intentfactbackcall.MyCallBack;
import com.mystyle.le.weekproject.utils.ParseJson;
import com.mystyle.le.weekproject.utils.Url;

import java.util.ArrayList;
import java.util.List;

/**
 * 排行页面
 * A simple {@link Fragment} subclass.
 */
public class SeniorityFragment extends Fragment {

    private ListView SeniorityFragment_LV;
    private SeniorityFragmentAdapter adapter;
    private int pageNum = 0;//当前请求数据是第几页
    //是否到底部
    private boolean isLast =false;
    private Button btn_add;
    private List<Seniority> lists = new ArrayList<>();
    private SeniorityFragmentAdapter seniorityFragmentAdapter;


    public SeniorityFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_seniority, container, false);
        /*
        * 初始化控件,并展示数据
        * */
        initView(view);
        /*
        * 点击跳转
        * */
        initChange();
        return view;
    }

    private void initChange() {
        //点击每一个item跳转到详情页
        SeniorityFragment_LV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Seniority seniority = lists.get(position);
                String id1 = seniority.getId();
                Intent intent = new Intent(getActivity(), DetialActivity.class);
                intent.putExtra("SeniorityFragment",id1);
                startActivity(intent);
            }
        });
    }

    private void initView(View view) {
        SeniorityFragment_LV = ((ListView) view.findViewById(R.id.SeniorityFragment_LV));
        btn_add = ((Button) view.findViewById(R.id.btn_add));
        seniorityFragmentAdapter = new SeniorityFragmentAdapter(lists, getActivity());
        new SeniorityFragmentAsynctaks(new MyCallBack.HeadListBack() {
          @Override
          public void sendBack(String path) {
              List<Seniority> seniorityList = ParseJson.parseSeniority(path);
              if (seniorityList!=null){
                  lists.addAll(seniorityList);
                  SeniorityFragment_LV.setAdapter(seniorityFragmentAdapter);
              }
          }
      },getActivity() ).execute(Url.getRecommendMore(pageNum,4));

        /*
        * 滚动监听
        * */
        SeniorityFragment_LV.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (isLast&&scrollState ==SCROLL_STATE_IDLE){
                    //到底部且滚动状态静止
                    btn_add.setVisibility(View.VISIBLE);
                }else {
                    btn_add.setVisibility(View.GONE);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                // 判断是否到底部
                isLast = (firstVisibleItem+visibleItemCount)==totalItemCount;
            }
        });

        /*
        * 加载更多
        * */
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pageNum++;
                new SeniorityFragmentAsynctaks(new MyCallBack.HeadListBack() {
                    @Override
                    public void sendBack(String path) {
                        List<Seniority> seniorityList = ParseJson.parseSeniority(path);
                        if (seniorityList!=null){
                            lists.addAll(seniorityList);
                           seniorityFragmentAdapter.notifyDataSetChanged();
                        }
                    }
                }, getActivity()).execute(Url.getRecommendMore(pageNum,4));
                Log.e("BBBB","==pageNum=="+pageNum);
//                btn_add.setVisibility(View.GONE);
            }
        });

    }
}
